var util = require('../../utils/util.js');
var app = getApp();
const users = require('../../config.js').users
Page({
    data: {
        showTopTips: false,
        errorMsg: ""
    },
    onLoad: function() {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    windowHeight: res.windowHeight,
                    windowWidth: res.windowWidth
                })
            }
        });
    },
    register: function(e) {

        wx.navigateTo({
            url: '../register/register'
        })
    },
    formSubmit: function(e) {
        // form 表单取值，格式 e.detail.value.name(name为input中自定义name值) ；使用条件：需通过<form bindsubmit="formSubmit">与<button formType="submit">一起使用
        var account = e.detail.value.account;
        var password = e.detail.value.password;
        var that = this;
        // 判断账号是否为空和判断该账号名是否被注册
        if ("" == util.trim(account)) {
            util.isError("账号不能为空", that);
            return;
        }
        // 判断密码是否为空
        if ("" == util.trim(password)) {
            util.isError("密码不能为空", that);
            return;
        } else {
            util.clearError(that);
        }

        // 验证都通过了执行注册方法


        wx.request({
            url: users,
            method: 'post',
            dataType: 'json',
            data: { username: account, password: password },
            header: {
                'content-type': 'application/json'
            },

            success: function(result) {

                console.log(result)
                if (result.data.code == '1') {
                    util.isError(result.data.msg, that);
                } else {

                    wx.setStorageSync("userId", result.data.obj.userId)
                    wx.showToast({
                        title: '登录成功',
                        duration: 2000
                    })

                }


            },
            fail: function(error) {
                wx.showToast({
                    title: '登录失败',
                    icon: 'error',
                    duration: 2000
                })
            }
        })

    }
})